﻿using Google.Protobuf.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class MomentWidget : MonoBehaviour
{
    public Text momentsNum;
    public Text content;
    public Text commentsNum;

    public MomentPhotoItem[] photos;

    public Text likeNum;
    public Image likeImg;

    UserMoment m_UserMoment;

    public void Init(UserMoment UserMoment,int totalMomentsCount)
    {
        m_UserMoment = UserMoment;
        momentsNum.text = $"({totalMomentsCount})";
        content.text = UserMoment.moment.content;

        if (m_UserMoment == null)
            return;

        var moment = m_UserMoment.moment;

        GameDebug.Log($"MomentWidget:{moment != null}");

        if (moment == null) return;

        int photoCount = 0;
        if (moment.texs != null)
        {
            for (int i = 0; i < 3; i++)
            {
                if (i >= moment.texs.Length)
                {
                    photos[i].gameObject.SetActive(false);
                    continue;
                }

                var spr = moment.texs[i];
                if (!string.IsNullOrEmpty(spr))
                {
                    photoCount++;
                    photos[i].Init(spr);
                }
                else
                {
                    photos[i].gameObject.SetActive(false);
                }
            }
        }

        RefreshLike(moment.likes);

        if (moment.comments != null)
            commentsNum.text = moment.comments.Count.ToString();
    }
    public void OnClickShare()
    {
        GameDebug.Log("CommunityItem->OnClickShare");
    }
    void RefreshLike(List<long> likes)
    {
        if (likes != null)
        {
            likeNum.text = likes.Count.ToString();

            bool myLike = false;
            for (int i = 0; i < likes.Count; i++)
            {
                if (likes[i] == MGame.instance.MUser.id)
                {
                    myLike = true;
                    break;
                }
            }
            likeImg.gameObject.SetActive(myLike);
        }
        else
        {
            likeNum.text = "0";
            likeImg.gameObject.SetActive(false);
        }
    }
    public void OnClickLike()
    {
        var userMoment = m_UserMoment.moment;

        RequestServerController.instance.ReqLike(userMoment.userId, userMoment.momentId, (res) =>
        {
            var list = new List<long>(userMoment.likes);
            list.Add(MGame.instance.MUser.id);
            userMoment.likes = list;
            RefreshLike(userMoment.likes);
        });
    }

    public void OnClickComments()
    {
        GameDebug.Log("MomentWidget -> OnClickComments");
        var momentPanel = GetComponentInParent<UIController>().ShowUI<MomentPanel>();
        momentPanel.Init(this.m_UserMoment);
    }

    public void OnClickAddMoment()
    {
        GetComponentInParent<UIController>().ShowUI<AddMomentPanel>();
    }

    public void OnClickShowUserMoments()
    {
        var userHome = GetComponentInParent<UserHomePanel>();
        userHome.ShowUserMomentsPanel();
    }
}
